package com.jn.mvvm2.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jn.mvvm2.databinding.QandaItemBinding;
import com.jn.mvvm2.entity.QAndAEntity;

import java.util.List;

public class QAndAAdapter extends RecyclerView.Adapter<QAndAAdapter.ViewHolder> {
    private List<QAndAEntity> mList;

    public QAndAAdapter(List<QAndAEntity> list) {
        mList = list;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void updateData(List<QAndAEntity> list) {
        mList.clear();
        mList = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        QandaItemBinding binding = QandaItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        QAndAEntity entity = mList.get(position);
        holder.binding.setData(entity);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        QandaItemBinding binding;

        public ViewHolder(QandaItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
